{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f84ae59b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import requests\n",
    "import time\n",
    "import os\n",
    "from IPython.display import clear_output # to clear screen while it print results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b467511b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# documentation https://docs.ftx.com/#spot-margin  \n",
    "# returns:\n",
    "# coin\tstring\tUSD\t\n",
    "# time\tstring\t2021-04-06T20:00:00+00:00\t\n",
    "# rate\tnumber\t0.00002283\thourly lending rate for this cycle\n",
    "# size\tnumber\t615974048.2224404\ttotal size matched between borrowers and lenders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30b38274",
   "metadata": {},
   "outputs": [],
   "source": [
    "BASE_DIR = os.path.abspath(os.path.join(os.getcwd(), \"..\", \"..\"))\n",
    "OUTPUT_DIR = os.path.join(BASE_DIR, \"build\", \"output\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6dd6244",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_appended = pd.DataFrame()\n",
    "\n",
    "# starts in late November 2020, so start with Dec 1 2020\n",
    "timeQuery = 1606842000 \n",
    "\n",
    "timeNow = int( time.time() )\n",
    "\n",
    "while (timeQuery < timeNow):\n",
    "    \n",
    "    endQuery = timeQuery + 86400-1 #seconds in a day = 24 * 60 * 60\n",
    "    fundingQuery = 'https://ftx.com/api/spot_margin/history?start_time=' + str(timeQuery)+ '&end_time=' + str(endQuery)\n",
    "    fundingRates = requests.get(fundingQuery).json()\n",
    "    fundingRates = pd.DataFrame(fundingRates['result']).sort_values('time', ascending=False)\n",
    "    \n",
    "    if len(fundingRates) == 0:\n",
    "        break\n",
    "\n",
    "    # interest rates are hourly, so to annualize multiply by dailyrate*24*365\n",
    "    fundingRates['apr'] = fundingRates['rate']*24*365\n",
    "\n",
    "    df_appended = df_appended.append(fundingRates, ignore_index=True)\n",
    "\n",
    "    timeQuery = endQuery+1\n",
    "    print(fundingRates['time'].iloc[[-1]].values[0])\n",
    "    clear_output(wait=True)\n",
    "    \n",
    "    time.sleep(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7364775d",
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = f'lending_rates_ftx.csv'\n",
    "df_appended.to_csv(os.path.join(OUTPUT_DIR,filename), index = False, header=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "leverage_and_stablecoin_pegs",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
